{% extends 'base.html' %}
{% load widget_tweaks %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h4 class="mb-0">
                        <i class="fas fa-university"></i> {{ title }}
                    </h4>
                </div>
                <div class="card-body">
                    <form method="post" novalidate>
                        {% csrf_token %}
                        
                        {% if form.non_field_errors %}
                            <div class="alert alert-danger">
                                {{ form.non_field_errors }}
                            </div>
                        {% endif %}

                        <div class="row">
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="{{ form.name.id_for_label }}" class="form-label">
                                        {{ form.name.label }} <span class="text-danger">*</span>
                                    </label>
                                    {{ form.name|add_class:"form-control" }}
                                    {% if form.name.errors %}
                                        <div class="invalid-feedback d-block">
                                            {{ form.name.errors.0 }}
                                        </div>
                                    {% endif %}
                                    {% if form.name.help_text %}
                                        <div class="form-text">{{ form.name.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="{{ form.code.id_for_label }}" class="form-label">
                                        {{ form.code.label }} <span class="text-danger">*</span>
                                    </label>
                                    {{ form.code|add_class:"form-control" }}
                                    {% if form.code.errors %}
                                        <div class="invalid-feedback d-block">
                                            {{ form.code.errors.0 }}
                                        </div>
                                    {% endif %}
                                    {% if form.code.help_text %}
                                        <div class="form-text">{{ form.code.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="{{ form.department.id_for_label }}" class="form-label">
                                        {{ form.department.label }} <span class="text-danger">*</span>
                                    </label>
                                    {{ form.department|add_class:"form-control" }}
                                    {% if form.department.errors %}
                                        <div class="invalid-feedback d-block">
                                            {{ form.department.errors.0 }}
                                        </div>
                                    {% endif %}
                                    {% if form.department.help_text %}
                                        <div class="form-text">{{ form.department.help_text }}</div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.description.id_for_label }}" class="form-label">
                                {{ form.description.label }}
                            </label>
                            {{ form.description|add_class:"form-control" }}
                            {% if form.description.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.description.errors.0 }}
                                </div>
                            {% endif %}
                            {% if form.description.help_text %}
                                <div class="form-text">{{ form.description.help_text }}</div>
                            {% endif %}
                        </div>

                        <div class="d-flex justify-content-between">
                            <a href="{% url 'accounts:college_list' %}" class="btn btn-secondary">
                                <i class="fas fa-arrow-left"></i> 返回列表
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> {{ action }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            {% if college %}
            <div class="card mt-4">
                <div class="card-header">
                    <h5 class="mb-0">
                        <i class="fas fa-info-circle"></i> 学院信息
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <p><strong>创建时间:</strong> {{ college.created_at|date:"Y-m-d H:i:s" }}</p>
                            <p><strong>更新时间:</strong> {{ college.updated_at|date:"Y-m-d H:i:s" }}</p>
                        </div>
                        <div class="col-md-6">
                            <p><strong>关联用户数:</strong> {{ college.userprofile_set.count }}</p>
                            <p><strong>关联子项目数:</strong> {{ college.subproject_set.count }}</p>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 学院代码自动转换为大写
    const codeInput = document.getElementById('{{ form.code.id_for_label }}');
    if (codeInput) {
        codeInput.addEventListener('input', function() {
            this.value = this.value.toUpperCase();
        });
    }
    
    // 表单验证
    const form = document.querySelector('form');
    form.addEventListener('submit', function(e) {
        let isValid = true;
        
        // 验证学院名称
        const nameInput = document.getElementById('{{ form.name.id_for_label }}');
        if (!nameInput.value.trim()) {
            isValid = false;
            nameInput.classList.add('is-invalid');
        } else {
            nameInput.classList.remove('is-invalid');
        }
        
        // 验证学院代码
        const codeInput = document.getElementById('{{ form.code.id_for_label }}');
        if (!codeInput.value.trim()) {
            isValid = false;
            codeInput.classList.add('is-invalid');
        } else {
            codeInput.classList.remove('is-invalid');
        }
        
        if (!isValid) {
            e.preventDefault();
        }
    });
});
</script>
{% endblock %}